package com.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.entity.School;
import com.entity.Student;
import com.service.ISchoolService;
import com.service.IStudentService;
import com.service.impl.SchoolService;
import com.service.impl.StudentService;
import com.tools.MyTools;
import com.tools.Page;

/**
 * student action
 * @author Administrator
 *
 */
public class StudentAction {
	
	private ISchoolService schoolService;
	private IStudentService studentService;
	
	private Student student;
	
	private List<Student> list;
	
	private Page page;
	
	private List<School> schList;
	
	
	/**
	 * 初始化page分页
	 */
	private void pageInit(){
		if(page==null){
			page=new Page(1, 15);
		}
		else{
			if(page.getSize()==0 || page.getSize()>100){
				page.setSize(15);
			}
		}
	}
	
	/**
	 * 添加
	 * @return
	 */
	public String add(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		session.setAttribute("", "");
		pageInit();
		studentService=new StudentService();
		student.setId(MyTools.getId(20));
		if(studentService.add(student)){
		}
		return "success";
	}
	
	public String to(){
		return "success";
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String update(){
		pageInit();
		studentService=new StudentService();
		if(studentService.update(student)){
		}
		studentService.count(page);
		
		
		return "success";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		pageInit();
		System.out.println(student);
		studentService=new StudentService();
		if(studentService.delete(student)){
		}
		studentService.count(page);
		
		
		return "success";
	}
	
	
	/**
	 * 查询一个
	 * @return
	 */
	public String findById(){
		pageInit();
		System.out.println(page);
		
		studentService=new StudentService();
		student=studentService.findById(student.getId());
		
		Page page1=new Page(1, 999);
		schoolService=new SchoolService();
		schList = schoolService.getList(page1);
		studentService.count(page);
		return "success";
	}
	
	/**
	 * 查询一组
	 * @return
	 */
	public String getAll(){
		
		pageInit();
		studentService=new StudentService();
		list=studentService.getList(page);
		
		return "success";
	}
	
	
	
	

	public List<School> getSchList() {
		return schList;
	}

	public void setSchList(List<School> schList) {
		this.schList = schList;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public List<Student> getList() {
		return list;
	}

	public void setList(List<Student> list) {
		this.list = list;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	
	
	
}
